package com.ugoodtech.mdcc.core.utils;


import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class BaseUtil{
    public static String getRandomString(int number) {
        String random = null;
        for (int i = 0; i < number; i++) {
            Integer ran = (int) (Math.random() * 10);
            if (0 < ran && ran < 10) {
                if (random == null) {
                    random = ran.toString();
                } else {
                    random = random + ran;
                }
            } else {
                i--;
            }
        }
        return random;
    }


    public static String priceToString(Float priceFloat) throws Exception {
        Double priceDouble;
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String priceStr = null;
        if (null != priceFloat) {
            priceDouble = (double) priceFloat;
            priceStr = decimalFormat.format(priceDouble);
        }
        return priceStr;

    }

    public static String priceToString(Long priceLong) throws Exception {
        Double priceDouble;
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String priceStr = null;
        if (null != priceLong) {
            priceDouble = (double) priceLong / 100;
            priceStr = decimalFormat.format(priceDouble);
        }
        return priceStr;

    }

    public static Long priceToLong(String priceStr) throws Exception {
        Double priceDouble;
        Long priceLong = null;
        if (null != priceStr) {
            priceDouble = (Double.parseDouble(priceStr)) * 100;
            priceLong = priceDouble.longValue();
        }
        return priceLong;

    }


    public static byte[] fileToByte(String url, String type) throws Exception {
        File file = new File(url);
        FileInputStream fis = new FileInputStream(file);
        byte[] b;
        b = new byte[fis.available()];
        fis.read(b);
        return b;
    }

    public static boolean createFile(File file) throws IOException {
        if (!file.exists()) {
            makeDir(file.getParentFile());
        }
        return file.createNewFile();
    }

    public static void makeDir(File dir) {
        if (!dir.getParentFile().exists()) {
            makeDir(dir.getParentFile());
        }
        dir.mkdir();
    }

    public static String IntDivideZero(Double before) {
        if (null != before) {
            int after = (int) before.doubleValue();
            if (before == after) {
                return after + "";
            } else {
                return before + "";
            }
        } else {
            return "/";
        }

    }

    public static String IntDivideZero(float before) {
        int after = (int) before;
        if (before == after) {
            return after + "";
        } else {
            return before + "";
        }
    }

    public static byte[] readBytes(InputStream is, int contentLen) {
        if (contentLen > 0) {
            int readLen = 0;

            int readLengthThisTime = 0;

            byte[] message = new byte[contentLen];

            try {

                while (readLen != contentLen) {

                    readLengthThisTime = is.read(message, readLen, contentLen
                            - readLen);

                    if (readLengthThisTime == -1) {// Should not happen.
                        break;
                    }

                    readLen += readLengthThisTime;
                }

                return message;
            } catch (IOException e) {
                // Ignore
                // e.printStackTrace();
            }
        }

        return new byte[]{};
    }


    public static <T> List<T> getModelList(CrudRepository<T, Long> repository, List<Long> ids){
        List<T> tList=new ArrayList<>();
        if(null!=ids){
            for(Long id:ids){
                T savedT= repository.findOne(id);
                tList.add(savedT);
            }
        }
        return tList;
    }

    public static <T> Set<T> getModelSet(CrudRepository<T, Long> repository, Long[] ids){
        Set<T> tList=new HashSet<>();
        for(Long id:ids){
            T savedT= repository.findOne(id);
            tList.add(savedT);
        }
        return tList;
    }

    public static <T>Page getDtoPage(Page<T> pageData, Pageable pageable, Class cls)  {
        Page<Object> pageResponses= null;
        try {
            List<Object> dtoData=new ArrayList();
            for(T data:pageData){
                Constructor c1 =cls.getDeclaredConstructor(data.getClass());
                Object b = c1.newInstance(data);
                dtoData.add(b);
            }
            pageResponses = new PageImpl(dtoData,pageable,pageData.getTotalElements());
        } catch (Exception e) {
            e.printStackTrace();
            return pageData;
        }
        return pageResponses;
    }


}
